const BaseController = require('./base');
const moment = require('moment');

class ExamController extends BaseController {
  async addTestPaper() {
    const { ctx } = this;
    ctx.validate({
      name: { type: 'string' },
      content: { type: 'content' },
    });
    const testPaperInfo = ctx.request.body;
    const res = await ctx.service.exam.addTestPaper(testPaperInfo);
    if (res.state) {
      this.success(res.msg);
    } else {
      this.fail(res.msg);
    }
  }

  async updateTestPaper() {
    const { ctx } = this;
    ctx.validate({
      id: { type: 'string' },
      name: { type: 'string' },
      content: { type: 'content' },
    });
    const testPaperInfo = ctx.request.body;
    const res = await ctx.service.exam.updateTestPaper(testPaperInfo);
    if (res.state) {
      this.success(res.msg);
    } else {
      this.fail(res.msg);
    }
  }

  async delTestPaper() {
    const { ctx } = this;
    const ids = ctx.request.body;
    if (!ids || !(ids instanceof Array)) {
      this.fail('参数错误');
      return;
    }
    const res = await ctx.service.exam.delTestPaper(ids);
    if (res.state) {
      this.success(res.msg);
    } else {
      this.fail(res.msg);
    }
  }

  async getTestPaper() {
    const { ctx } = this;
    const { id, pageNum, pageSize, name = '' } = ctx.query;
    if (id) {
      const res = await ctx.service.exam.getTestPaper(id);
      if (res) {
        this.success(res);
      } else {
        this.fail('试卷不存在');
      }
    } else {
      const res = await ctx.service.exam.getTestPapers(Number(pageNum), Number(pageSize), name);
      if (res) {
        this.success(res);
      } else {
        this.fail('查询失败');
      }
    }
  }


  async addExam() {
    const { ctx } = this;
    ctx.validate({
      name: { type: 'string' },
      begin: { type: 'string' },
      end: { type: 'string' },
      duration: { type: 'number' },
    });
    const testInfo = ctx.request.body;
    const beginMoment = moment(testInfo.begin);
    const endMoment = moment(testInfo.end);
    if (beginMoment.isAfter(endMoment)) {
      return this.fail('开始时间不能大于结束时间');
    }
    const durationMinutes = endMoment.diff(beginMoment, 'minutes');
    if (durationMinutes < testInfo.duration) {
      return this.fail('考试时长不合法');
    }
    const result = await ctx.service.exam.addExam(testInfo);
    if (result) {
      return this.success('添加成功');
    }
    return this.fail('添加失败');

  }

  async getExams() {
    const { ctx } = this;
    const { id, pageNum = 1, pageSize = 10, name = '' } = ctx.query;
    if (id) {
      const res = await ctx.service.exam.getExam(id);
      if (res) {
        return this.success(res);
      }
      return this.fail('获取失败');
    }
    const result = await ctx.service.exam.getExams(
      Number(pageNum),
      Number(pageSize),
      name
    );
    if (result) {
      return this.success(result);
    }
    return this.fail('获取失败');
  }

  async updateExam() {
    const { ctx } = this;
    ctx.validate({
      id: { type: 'string' },
      name: { type: 'string' },
      begin: { type: 'string' },
      end: { type: 'string' },
      duration: { type: 'number' },
    });
    const testInfo = ctx.request.body;
    const beginMoment = moment(testInfo.begin);
    const endMoment = moment(testInfo.end);
    if (beginMoment.isAfter(endMoment)) {
      return this.fail('开始时间不能大于结束时间');
    }
    const durationMinutes = endMoment.diff(beginMoment, 'minutes');
    if (durationMinutes < testInfo.duration) {
      return this.fail('考试时长不合法');
    }
    const result = await ctx.service.exam.updateExam(testInfo);
    if (result) {
      return this.success('更新成功');
    }
    return this.fail('更新失败');
  }

  async addExamUser() {
    const { ctx } = this;
    ctx.validate({
      examId: { type: 'string' },
      userIds: { type: 'array' },
    });
    const { examId, userIds } = ctx.request.body;
    const result = await ctx.service.exam.addExamUser(examId, userIds);
    if (result) {
      return this.success('添加成功');
    }
    return this.fail('添加失败');
  }

  async delExamUsers() {
    const { ctx } = this;
    ctx.validate({
      examId: { type: 'string' },
      userIds: { type: 'array' },
    });
    const { examId, userIds } = ctx.request.body;
    const result = await ctx.service.exam.delExamUsers(examId, userIds);
    if (result) {
      return this.success('删除成功');
    }
    return this.fail('删除失败');
  }

  async sendEmail() {
    const { ctx } = this;
    ctx.validate({
      examId: { type: 'string' },
      userId: { type: 'string' },
    });
    const { examId, userId } = ctx.request.body;
    const result = await ctx.service.exam.sendEmail(examId, userId);
    if (result.state) {
      return this.success({
        type: 'success',
        msg: result.msg,
      });
    }
    return this.success({
      type: 'error',
      msg: result.msg,
    });
  }

  async getNum() {
    const { ctx } = this;
    const res = await ctx.service.exam.getNum();
    if (res) {
      return this.success(res);
    }
    return this.fail('获取失败');
  }

  async delExam() {
    const { ctx } = this;
    const ids = ctx.request.body;
    if (!ids || !(ids instanceof Array)) {
      this.fail('参数错误');
      return;
    }
    const res = await ctx.service.exam.delExam(ids);
    if (res.state) {
      this.success(res.msg);
    } else {
      this.fail(res.msg);
    }
  }

  async updateConfig() {
    const { ctx } = this;
    ctx.validate({
      examId: { type: 'string' },
      is_limit_jump: { type: 'number' },
      face_recording: { type: 'number' },
      limit_jump_times: { type: 'number' },
      screen_recording: { type: 'number' },
    });
    const { examId, is_limit_jump, face_recording, limit_jump_times, screen_recording } = ctx.request.body;
    const result = await ctx.service.exam.updateConfig(examId, is_limit_jump, face_recording, limit_jump_times, screen_recording);
    if (result) {
      return this.success('更新成功');
    }
    return this.fail('更新失败');
  }

  async linkExamPaper() {
    const { ctx } = this;
    ctx.validate({
      examId: { type: 'string' },
      testPaperIds: { type: 'array' },
    });
    const { examId, testPaperIds } = ctx.request.body;
    const result = await ctx.service.exam.linkExamPaper(examId, testPaperIds);
    if (result) {
      return this.success('关联成功');
    }
    return this.fail('关联失败');
  }
}

module.exports = ExamController;
